package com.gzbugu.bugusys.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.RequestAware;

import com.gzbugu.bugusys.bean.PageModel;
import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.service.FindAttendanceService;
import com.opensymphony.xwork2.ActionSupport;

public class FindAttendanceAction extends ActionSupport implements RequestAware {
	private int year;
	private int month;
	private String staffId;
	private Date start;
	private Date end;
	private PageModel page;
	private int payment;
	private List typeList;
	private List errorList;
	private List attendanceList;
	private Map<String, Object> request;
	String methodName;
	@Resource(name = "findAttendanceService")
	FindAttendanceService attendanceService;
	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;

	public String findById() {
		if (this.invalidate1()) {
			if (page == null)
				page = new PageModel();
			page.setLineCount(attendanceService.countById(staffId));
			this.setAttendanceList(attendanceService.findById(staffId, page));
			this.setPayment(attendanceService.countPay(staffId));
			this.setTypeList(attendanceService.countTypes(staffId));
			methodName = "queryById?staffId=" + staffId + "&";
			request.put("methodName", methodName);
		}
		return "success";
	}

	public String findByTime() {
		if (this.invalidate2()) {
			if (page == null)
				page = new PageModel();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			page.setLineCount(attendanceService.countByTime(start, end));
			this.setAttendanceList(attendanceService.findByTime(start, end,
					page));
			this.setPayment(attendanceService.countPay(start, end));
			this.setTypeList(attendanceService.countTypes(start, end));
			methodName = "queryByTime?start=" + df.format(start) + "&end="
					+ df.format(end) + "&";
			request.put("methodName", methodName);
		}
		return "success";
	}

	public String findByMonth() {
		if (this.invalidate3()) {
			if (page == null)
				page = new PageModel();
			page.setLineCount(attendanceService.countByMonth(year, month));
			this.setAttendanceList(attendanceService.findByMonth(year, month,
					page));
			this.setPayment(attendanceService.countPay(year, month));
			this.setTypeList(attendanceService.countTypes(year, month));
			methodName = "queryByMonth?year=" + String.valueOf(year)
					+ "&month=" + String.valueOf(month) + "&";
			request.put("methodName", methodName);
		}
		return "success";
	}

	public String findAll() {
		if (page == null)
			page = new PageModel();
		page.setLineCount(attendanceService.countAll());
		this.setAttendanceList(attendanceService.findAll(page));
		this.setPayment(attendanceService.countPay());
		this.setTypeList(attendanceService.countTypes());
		methodName = "queryAll?";
		request.put("methodName", methodName);
		return "success";
	}

	public boolean invalidate1() {
		boolean flag = true;
		errorList = new ArrayList();
		if (this.staffId == null && "".equals(this.staffId)) {
			this.errorList.add("请输入员工编号再查询！");
			flag = false;
		} else if (baseDAO.findById(staffId,
				com.gzbugu.bugusys.fields.VoClasseFields.STAFF) == null) {
			this.errorList.add("编号为" + staffId + "的员工不存在！");
			flag = false;
		}
		return flag;
	}

	public boolean invalidate2() {
		boolean flag = true;
		errorList = new ArrayList();
		if (this.start == null || "".equals(this.start)) {
			this.errorList.add("请输入开始时间再查询！");
			flag = false;
		}
		if (this.end == null || "".equals(this.end)) {
			this.errorList.add("请输入结束时间再查询！");
			flag = false;
		}
		return flag;
	}

	public boolean invalidate3() {
		boolean flag = true;
		errorList = new ArrayList();
		if (this.year < 2012) {
			this.errorList.add("年份输入有误");
			flag = false;
		}
		if (this.month <= 0 || this.month > 12) {
			this.errorList.add("月份输入有误");
			flag = false;
		}
		return flag;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public String getStaffId() {
		return staffId;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public PageModel getPage() {
		return page;
	}

	public void setPage(PageModel page) {
		this.page = page;
	}

	public List getErrorList() {
		return errorList;
	}

	public void setErrorList(List errorList) {
		this.errorList = errorList;
	}

	public List getAttendanceList() {
		return attendanceList;
	}

	public void setAttendanceList(List attendanceList) {
		this.attendanceList = attendanceList;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public int getPayment() {
		return payment;
	}

	public void setPayment(int payment) {
		this.payment = payment;
	}

	public List getTypeList() {
		return typeList;
	}

	public void setTypeList(List typeList) {
		this.typeList = typeList;
	}

}
